package com.jhclothing.product.service.impl;

import com.jhclothing.common.constant.UserConstants;
import com.jhclothing.common.enums.ClothingSex;
import com.jhclothing.common.exception.ServiceException;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.product.domain.ClothingFabricRelation;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.mapper.ClothingFabricRelationMapper;
import com.jhclothing.product.mapper.ClothingInventoryMapper;
import com.jhclothing.product.mapper.ClothingTypeMapper;
import com.jhclothing.product.service.IClothingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服饰品类Service业务层处理
 *
 * @author zyp
 * @date 2024-04-21
 */
@Service
public class ClothingTypeServiceImpl implements IClothingTypeService {
    @Autowired
    private ClothingTypeMapper clothingTypeMapper;

    @Autowired
    private ClothingFabricRelationMapper clothingFabricRelationMapper;

    @Autowired
    private ClothingInventoryMapper clothingInventoryMapper;

    /**
     * 查询服饰品类
     *
     * @param clothingTypeId 服饰品类主键
     * @return 服饰品类
     */
    @Override
    public ClothingType selectClothingTypeByClothingTypeId(Long clothingTypeId) {
        ClothingType clothingType = clothingTypeMapper.selectClothingTypeByClothingTypeId(clothingTypeId);
        String clothingSexName = ClothingSex.getClothingSexNameByCode(clothingType.getClothingSex());
        if (StringUtils.isNotEmpty(clothingSexName)) {
            clothingType.setClothingTypeCodeAndSex(clothingType.getClothingTypeCode() + " - " + clothingSexName);
        } else {
            clothingType.setClothingTypeCodeAndSex(clothingType.getClothingTypeCode());
        }
        return clothingType;
    }

    /**
     * 查询服饰品类列表
     *
     * @param clothingType 服饰品类
     * @return 服饰品类
     */
    @Override
    public List<ClothingType> selectClothingTypeList(ClothingType clothingType) {
        List<ClothingType> clothingTypes = clothingTypeMapper.selectClothingTypeList(clothingType);
        List<ClothingType> clothingTypeAndFabrics = clothingTypeMapper.selectClothingTypeListAndFabrics(clothingType);

        for (ClothingType type : clothingTypes) {
            for (ClothingType clothingTypeAndFabric : clothingTypeAndFabrics) {
                if (type.getClothingTypeId() == clothingTypeAndFabric.getClothingTypeId()) {
                    type.setFabrics(clothingTypeAndFabric.getFabrics());
                }
            }
        }
        return clothingTypes;
    }

    /**
     * 查询所有服饰品类
     *
     * @return 服饰品类列表
     */
    @Override
    public List<ClothingType> selectClothingTypeAll() {
        List<ClothingType> clothingTypes = clothingTypeMapper.selectClothingTypeAll();
        clothingTypes.stream()
                .map(clothingType -> {
                    String clothingSexName = ClothingSex.getClothingSexNameByCode(clothingType.getClothingSex());
                    if (StringUtils.isNotEmpty(clothingSexName)) {
                        clothingType.setClothingTypeCodeAndSex(clothingType.getClothingTypeCode() + " - " + clothingSexName);
                    } else {
                        clothingType.setClothingTypeCodeAndSex(clothingType.getClothingTypeCode());
                    }
                    return clothingType;
                })
                .collect(Collectors.toList());
        return clothingTypes;
    }

    /**
     * 查询服饰品类编码+服饰类别的产品是否唯一
     *
     * @param clothingType 服饰品类信息
     * @return true，唯一  false，不唯一
     */
    public boolean checkClothingTypeCodeAndSexUnique(ClothingType clothingType) {
        Long clothingTypeId = (null == clothingType.getClothingTypeId()) ? -1L : clothingType.getClothingTypeId();
        ClothingType type = clothingTypeMapper.checkClothingTypeCodeAndSexUnique(clothingType.getClothingTypeCode(), clothingType.getClothingSex());
        if ((null != type) && type.getClothingTypeId().longValue() != clothingTypeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增服饰品类
     *
     * @param clothingType 服饰品类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertClothingType(ClothingType clothingType) {
        // 新增服饰品类信息
        int rows = clothingTypeMapper.insertClothingType(clothingType);
        // 新增服饰品类与布料关联信息
        insertClothingFabricRelation(clothingType);
        return rows;
    }

    /**
     * 新增服饰品类与布料关联信息
     *
     * @param clothingType 服饰品类
     */
    public void insertClothingFabricRelation(ClothingType clothingType) {
        Long[] fabricIds = clothingType.getFabricIds();
        if (StringUtils.isNotEmpty(fabricIds)) {
            // 新增服饰品类与布料关联信息
            List<ClothingFabricRelation> list = new ArrayList<ClothingFabricRelation>(fabricIds.length);
            for (Long fabricId : fabricIds) {
                ClothingFabricRelation cfr = new ClothingFabricRelation();
                cfr.setClothingTypeId(clothingType.getClothingTypeId());
                cfr.setFabricId(fabricId);
                list.add(cfr);
            }
            clothingFabricRelationMapper.batchClothingFabricRelation(list);
        }
    }

    /**
     * 修改服饰品类
     *
     * @param clothingType 服饰品类
     * @return 结果
     */
    @Transactional
    @Override
    public int updateClothingType(ClothingType clothingType) {
        // 删除服饰品类与布料关联
        clothingFabricRelationMapper.deleteClothingFabricRelationByClothingTypeId(clothingType.getClothingTypeId());
        // 新增服饰品类与布料关联
        insertClothingFabricRelation(clothingType);
        return clothingTypeMapper.updateClothingType(clothingType);
    }

    /**
     * 批量删除服饰品类
     *
     * @param clothingTypeIds 需要删除的服饰品类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteClothingTypeByClothingTypeIds(Long[] clothingTypeIds) {
        for (Long clothingTypeId : clothingTypeIds) {
            int useNum = clothingInventoryMapper.countClothingTypeByClothingTypeId(clothingTypeId);
            if (useNum > 0) {
                ClothingType clothingType = selectClothingTypeByClothingTypeId(clothingTypeId);
                String clothingSexName = ClothingSex.getClothingSexNameByCode(clothingType.getClothingSex());
                throw new ServiceException(String.format("服饰品类编码为 %s, 服饰类别为 %s 的服饰品类已分配，不能删除",
                        clothingType.getClothingTypeCode(), clothingSexName));
            }
        }
        // 删除服饰品类与布料关联信息
        clothingFabricRelationMapper.deleteClothingFabricRelation(clothingTypeIds);
        return clothingTypeMapper.deleteClothingTypeByClothingTypeIds(clothingTypeIds);
    }
}
